data structures dsu graphs

Please click on ads to support us..

Python Code:

in_il = lambda: list(map(int, input().split()))
in_sl = lambda: input().split()
in_i = lambda: int(input())
MAX_INT = float('inf')

def solve():
    n = in_i()
    s = input()
    ans = 1
    for i in range(1, len(s)):
        if s[i] == "(" and s[i-1] == "(":
            ans += 1
    print(ans)

for _ in range(in_i()):
    solve()
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define setbitcounts(x)   __builtin_popcountll(x)
#define ll                long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define pll               pair<long,long>
#define vii               vector<int,int>
#define vll               vector<long,long>
#define mii               map<int,int>
#define mci               map<char,int>
#define sti               set<int>
#define stc               set<char>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define rep2(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define mem0(a)           memset(a,0,sizeof(a))
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppcll             __builtin_popcountll
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
unordered_map<long long, int, custom_hash> safe_map;

// works for at least up to (2**31 &mdash; 1)**2
long long int_sqrt (long long x) {
    long long ans = 0;
    for (ll k = 1LL << 30; k != 0; k /= 2) {
    if ((ans + k) * (ans + k) <= x) {
        ans += k;
       }
    }
    return ans;
}

// Power function using modular exponentiation
ll powmod(ll a, ll b, ll p){
    a %= p;
    if (a == 0) return 0;
    ll product = 1;
    while(b > 0){
        if (b&1){    // you can also use b % 2 == 1
            product *= a;
            product %= p;
            --b;
        }
        a *= a;
        a %= p;
        b /= 2;    // you can also use b >> 1
    }
    return product;
}


const long long INF=1e18;
const int32_t M32=1e9+7;
const int32_t MM32=998244353;
const int N=0;
//ascii value A=65,Z=90,a=97,z=122

void solve(){     

}


// Abe saale always use long long, never ever use int in CP, 
// Tuzhe two times dokha diya hai isne yaad rakh saale.
// Also for Min and Max always use INT_MAX and INT_MIN respectively.
// Learn from your mistake pagal, kitane baar katvayega. 
// Give more time and focus on prob B in div2 as off now, 
// think again and again with pen and paper before coding it


// Always use 1 based index for prefix sum problems
// 1 based for all that it for given input vector too.
// Means store all elements from 1 to n only
// before 1 or after n we can have 0
// This is most imp : Always remember to use 1 based index
// in Prefix Sum problems otherwise it can get very messy
// Don't ever use in-built sqrt for CP, find squre root,
// using Binary search for large numbers


int main()
{
IOS
int t=1;
cin>>t;
while(t--){
//solve();
ll n; cin >> n;
string s; cin >> s;

if(n == 1){
    cout << 1 << endl;
    continue;
}

ll sz = s.size();
ll ans = 0;
rep(i,0,sz-1){
    if(s[i] == '(' and s[i+1] == '(')ans++;
}

cout << ans+1 << endl;

}

return 0;
}

/*
9
(())(((())))(()())


1
3
(()())


1
9
((())((()())()))()


*/


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST